<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hInput"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          default-expand-all
          node-key="id"
          :data="depts"
          :expand-on-click-node="false"
          :props="defaultProps"
          highlight-current
          @current-change="hCurrentChage"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showImportDialog=true">excel导入</el-button>
          <el-button size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employeeList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssignRole(row.id)">角色</el-button>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small" style="margin-left:10px">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            :total="total"
            :page-size="queryObj.pagesize"
            :current-page="queryObj.page"
            layout="total,prev, pager, next"
            @current-change="currentChange"
          />
        </el-row>
      </div>
    </div>
    <importList :show-import-dialog.sync="showImportDialog" @load="getEmployee" />
    <AssignRole ref="refRole" :show-role-dialog.sync="showRoleDialog" :cur-id="curId" />
  </div>
</template>

<script>
import { arr2tree } from '@/utils'
import { getDepartment } from '@/api/department'
import { getEmployeeList, exportList, delEmployee } from '@/api/employee'
import fileSaver from 'file-saver'
import importList from './components/importList.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: { importList, AssignRole },
  data() {
    return {
      curId: -1,
      showRoleDialog: false,
      showImportDialog: false,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryObj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: ''
      },
      total: 0,
      employeeList: []
    }
  },
  created() {
    this.getDepatrment()
  },
  methods: {
    async hDel(id) {
      await delEmployee(id)
      this.$message.success('删除成功')
      if (this.employeeList.length === 1) {
        if (this.queryObj.page > 1) this.queryObj.page--
      }
      this.getDepatrment()
    },
    hInput() {
      this.queryObj.page = 1
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.getEmployee()
      }, 500)
    },
    async getDepatrment() {
      const res = await getDepartment()
      this.depts = arr2tree(res.data, 0)
      const id = this.depts[0].id
      this.queryObj.departmentId = id
      this.getEmployee()
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(id)
      })
    },
    hCurrentChage(data) {
      this.queryObj.departmentId = data.id
      this.getEmployee()
    },
    async getEmployee() {
      const res = await getEmployeeList(this.queryObj)
      this.total = res.data.total
      this.employeeList = res.data.rows
    },
    currentChange(page) {
      this.queryObj.page = page
      this.getEmployee()
    },
    async hExport() {
      const res = await exportList()
      fileSaver.saveAs(res, '员工信息表.xlsx')
    },
    hAssignRole(id) {
      this.showRoleDialog = true
      this.curId = id
      this.$nextTick(_ => {
        this.$refs.refRole.getRolseById()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
